package com.mallbook.model.service;

import com.mallbook.model.exception.CategoryExitsException;
import com.mallbook.model.exception.CategoryNameMaxException;
import com.mallbook.model.exception.ServiceException;
import com.mallbook.model.pojo.Category;
import com.mallbook.model.mapper.CategoryMapper;
import com.mallbook.model.utils.MybatisUtils;
import org.apache.ibatis.exceptions.PersistenceException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-06-24
 * 商品类别Service
 */
public class CategoryService {

    /**
     * 新增商品类别
     *
     * @param name      类别名 不能重复，长度为2-5
     * @param isDisplay 是否显示 1代表显示，-1代表不显示，默认为1
     */
    public void addCategory(String name, Integer isDisplay) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        Category category = new Category();
        category.setCategoryName(name);
        // 查询该类别是否存在，如果存在则抛出异常
        if (name.length() > 5 || name.length() < 2) {
            throw new CategoryNameMaxException("类别名长度不符合规范");
        }
        int count = categoryMapper.findCategoryNumByName(category.getCategoryName());

        if (count > 0) {
            throw new CategoryExitsException("该类别已存在");
        }
        // 设置该名称为显示
        category.setIsDisplay(isDisplay);
        // 查询数据库中类别的最大序号
        int maxSort = categoryMapper.findCategoryMaxSort();
        // 将新类别添加到数据库
        category.setSort(maxSort + 1);
        category.setState(1);
        try {
            categoryMapper.add(category);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }

    }

    /**
     * 通过ID删除一条类别信息
     *
     * @param id 需要删除的类别名称的ID
     */
    public void deleteCategoryOfDateBase(int id) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        try {
            categoryMapper.delete(id);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 通过ID批量删除
     *
     * @param ids 需要删除的类别名称的ID的集合
     */
    public void deleteCategoryMoreOfDateBase(List<Integer> ids) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        try {
            categoryMapper.deleteMore(ids);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 修改 名称，序号，导航，删除标识
     *
     * @param name      名字
     * @param sort      排序
     * @param isDisplay 是否显示
     * @param state     状态
     * @param id        需要修改的类别名的ID
     */
    public void update(String name, Integer sort, Integer isDisplay, Integer state, Integer id) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        Category category = new Category();
        category.setCategoryName(name);
        category.setSort(sort);
        category.setIsDisplay(isDisplay);
        category.setState(state);
        try {
            //修改类别名： 判断类别名是否被修改，如果被修改false,未修改true;如果修改需要判断名称是否重复，且判断时不能与自身比较
            if (!categoryMapper.findCategoryById(id).getCategoryName().equals(name)) { // 如果名字不一样，则修改
                // 查询该类别是否存在，如果存在则抛出异常
                int count = categoryMapper.findCategoryNumByName(category.getCategoryName());
                if (count > 0) {
                    throw new CategoryExitsException("该类别已存在");
                }
            }
            categoryMapper.update(category, id);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }

    }

    /**
     * 删除一条类别信息（逻辑删除）
     *
     * @param id 类别ID
     */
    public void delCategory(Integer id) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        Category category = new Category();
        category.setState(-1);
        try {
            categoryMapper.update(category, id);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 删除多条类别信息（逻辑删除）
     *
     * @param ids 类别ID的集合
     */
    public void delCategoryMore(List<Integer> ids) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        Category category = new Category();
        category.setState(-1);
        try {
            categoryMapper.updateMore(ids, category);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 修改类别是否展示 1 代表展示 -1 代表不展示
     *
     * @param id      类别ID
     * @param display 1 代表展示 -1 代表不展示
     */
    public void displayCategory(Integer id, Integer display) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        Category category = new Category();
        category.setIsDisplay(display);
        try {
            categoryMapper.update(category, id);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 批量修改类别是否展示 1 代表展示 -1 代表不展示
     *
     * @param ids     类别ID的集合
     * @param display 1 代表展示 -1 代表不展示
     */
    public void displayCategoryMore(List<Integer> ids, Integer display) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        Category category = new Category();
        category.setIsDisplay(display);
        try {
            categoryMapper.updateMore(ids, category);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 查询所有商品类型
     *
     * @return 所有商品类型
     */
    public List<Category> getAllCategory() {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        try {
            return categoryMapper.findCategory();

        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 查询所有未删除商品类型
     *
     * @return 所有商品类型
     */
    public List<Category> getAllCategoryIsNotDel() {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        List<Category> categoryList;
        List<Category> categoryListIsNotDel = new ArrayList<>();
        try {
            categoryList = categoryMapper.findCategory();
            for (Category c : categoryList
            ) {
                if (c.getState() == 1) {
                    categoryListIsNotDel.add(c);
                }
            }

        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
        return categoryListIsNotDel;
    }

    /**
     * 通过ID查询分类
     * 用于：修改类别时需要查询原来的数据展示到页面
     *
     * @param id 需要查询的商品类型的id
     * @return 一条商品类型
     */
    public Category getCategoryById(int id) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        try {
            return categoryMapper.findCategoryById(id);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 查询所有未删除和未屏蔽的商品类型
     *
     * @return 所有商品类型
     */
    public List<Category> getAllCategoryIsNotDelAndDisplay() {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        List<Category> categoryList;
        List<Category> categoryListIsNotDelAndDisplay = new ArrayList<>();
        try {
            categoryList = categoryMapper.findCategory();
            for (Category c : categoryList
            ) {
                if (c.getState() == 1 && c.getIsDisplay() == 1) {
                    categoryListIsNotDelAndDisplay.add(c);
                }
            }

        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
        return categoryListIsNotDelAndDisplay;
    }

    /**
     * 判断类别名是否存在
     *
     * @param name
     * @return 如果存在，返回true，如果不存在，返回false
     */
    public boolean isExit(String name) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        try {
            return categoryMapper.findCategoryNumByName(name) != 0;
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 根据ID修改顺序
     *
     * @param sid 序号
     * @param cid 序号ID
     */
    public void updateSortById(int sid, int cid) {
        CategoryMapper categoryMapper = MybatisUtils.getMappers(CategoryMapper.class);
        try {
            categoryMapper.updateSortByID(sid, cid);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }
}




